const {
    MYSQL_HOST,
    MYSQL_PORT,
    MYSQL_DATABASE,
    MYSQL_USER,
    MYSQL_PASSWORD
} = require("../config/database");
const Sequelize = require("sequelize");
const { QueryTypes, DataTypes, Model,Deferrable } = require("sequelize");
var sequelizeObj = new Sequelize(MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD, {
    host: MYSQL_HOST,
    port: MYSQL_PORT,
    dialect: 'mysql',
    timezone: '+08:00',
    logging: false,
    pool: {
        max: 5,
        min: 0,
        idle:10000
    },
    define: {
        freezeTableName: true,
        timestamps: false,
        paranoid: false,
        operatorsAliases: false
    }
});
sequelizeObj.authenticate()
    .then(() => {
        console.log('Connection has been established successfully.');
    })
    .catch(err => {
        console.error('Unable to connect to the database:', err);
    });
const user = require('../model/user')(sequelizeObj, DataTypes, Model, Deferrable)
const role = require('../model/role')(sequelizeObj, DataTypes, Model)
const book = require('../model/book')(sequelizeObj, DataTypes, Model)
const bookImage = require('../model/bookImage')(sequelizeObj, DataTypes, Model)
const borrow = require('../model/borrow')(sequelizeObj, DataTypes, Model)
const comment=require("../model/comment")(sequelizeObj, DataTypes, Model)
user.sync({force: false})
    .then((result) => {
        console.log(result);
    })
    .catch((err) => {
        console.error(err);
    });
book.sync({force: false})
    .then((result) => {
        console.log(result);
    })
    .catch((err) => {
        console.error(err);
    });
bookImage.sync({force: false})
    .then((result) => {
        console.log(result);
    })
    .catch((err) => {
        console.error(err);
    });
borrow.sync({force: false})
    .then((result) => {
        console.log(result);
    })
    .catch((err) => {
        console.error(err);
    });
comment.sync({force: false})
    .then((result) => {
        console.log(result);
    })
    .catch((err) => {
        console.error(err);
    });
role.sync({force: false})
    .then((result) => {
        console.log(result);
    })
    .catch((err) => {
        console.error(err);
    });
borrow.belongsTo(book,{foreignKey:"bookid"})
module.exports = { user,book,bookImage,borrow,comment,role }